package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JList;
import java.awt.Panel;
import java.awt.Button;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.ScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import java.awt.SystemColor;
import java.io.IOException;
import java.util.ArrayList;
import entity.UserLoginInfo;
import controllers.ClientController;
import controllers.WorkerController;

//this class open the main login window of all user

public class LoginGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel Login;
	private JTextField textFieldID;
	private JPasswordField passwordField;
	public	String text;
	public final static ImageIcon IMG_LOGIN_BACKGROUND = new ImageIcon("images/LoginP.jpg");
	UserLoginInfo uLoginInfo;
	ClientController clientCont;
	private JTextField textFieldIP;
	private JTextField textFieldPort;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginGUI frame = new LoginGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginGUI() {
	//	this.setIconImage(IMG_LOGIN_BACKGROUND);  *********************************************************************

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 365);
		Login = new JPanel();
		Login.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(Login);
		Login.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setForeground(Color.BLUE);
		panel.setBackground(SystemColor.activeCaptionBorder);
		Login.add(panel);
		
		textFieldID = new JTextField();
		textFieldID.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//String text=textField.getText();
				
				
			}
		});
		textFieldID.addInputMethodListener(new InputMethodListener() {
			public void caretPositionChanged(InputMethodEvent arg0) {
			}
			public void inputMethodTextChanged(InputMethodEvent arg0) {
					
			}
		});
		
		
		textFieldID.setToolTipText("Enter ID");
		textFieldID.setBounds(97, 20, 120, 20);
		textFieldID.setColumns(10);
		
		JLabel lblUserid = new JLabel("UserID");
		lblUserid.setForeground(Color.BLUE);
		lblUserid.setBackground(Color.BLUE);
		lblUserid.setBounds(10, 23, 64, 14);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setForeground(Color.BLUE);
		lblPassword.setBackground(Color.BLUE);
		lblPassword.setBounds(10, 61, 64, 14);
		panel.setLayout(null);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {            /*login button action */
				try{
					btnLoginActionPerformed(e);
					
					/*	WorkerGUI frame = new WorkerGUI();
					frame.setVisible(true);  */
					
				}catch(IOException e1) {
					e1.printStackTrace();
				}
			
			}
			
		});
		
		
		
		btnLogin.setToolTipText("Confirm Login");
		btnLogin.setBounds(97, 226, 71, 23);
		panel.add(btnLogin);
		panel.add(lblUserid);
		panel.add(lblPassword);
		panel.add(textFieldID);
		
		passwordField = new JPasswordField();
		passwordField.setToolTipText("Enter Password");
		passwordField.setBounds(97, 57, 120, 23);
		panel.add(passwordField);
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.setBounds(220, 226, 76, 23);
		panel.add(btnCancel);
		
		JCheckBox chckbxRemmemberMe = new JCheckBox("remmember me");
		chckbxRemmemberMe.setBounds(139, 267, 120, 23);
		panel.add(chckbxRemmemberMe);
		
		JLabel lblIPAddress = new JLabel("IP Addrees");
		lblIPAddress.setBounds(10, 121, 64, 14);
		panel.add(lblIPAddress);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(10, 153, 46, 14);
		panel.add(lblPort);
		
		textFieldIP = new JTextField();
		textFieldIP.setBounds(97, 118, 120, 20);
		panel.add(textFieldIP);
		textFieldIP.setColumns(10);
		
		textFieldPort = new JTextField();
		textFieldPort.setBounds(96, 150, 121, 20);
		panel.add(textFieldPort);
		textFieldPort.setColumns(10);
	}
	
	
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	private void btnLoginActionPerformed(ActionEvent e) throws IOException  {
		
		int port=0;
		String IP=null;
		
		String ID=textFieldID.getText();         //get user id as inserted
		String pass=passwordField.getText();   // get user password as inserted
		
		if(ID.equals(""))
		{
			JOptionPane.showMessageDialog(null, "No ID is entered, Try again ");
		}
		else if (pass.equals("") ) 
		{
			JOptionPane.showMessageDialog(null, "No Password is entered, Try again");
		}
		else	if (    (!(ID.equals(""))) || (!( pass.equals("")))    )
		{
			uLoginInfo=new UserLoginInfo(ID,pass);             // save user login info as inserted 		
		}
	
		if(textFieldIP.getText().equals(""))
			IP="127.0.0.1";    									// default ip address
		else
			IP=textFieldIP.getText();
	
		if(textFieldPort.getText().equals(""))
			port=5555;    									// default port number
		else
			port=Integer.parseInt(textFieldPort.getText());	
	
		
		ArrayList<String> arrLogin=new ArrayList<String>();   //array use to keep info about the action ( login) and user info 
		arrLogin.add("Login");
		arrLogin.add(ID);
		arrLogin.add(pass);
		this.clientCont=new ClientController(IP, port);		
		clientCont.handleMessageFromGui(arrLogin);
		
	}
}
